from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from qcloud_cos.cos_exception import CosClientError, CosServiceError
import sys
import os
import logging



class Cos():
    def __init__(self, secret_id, secret_key, region):

        self.client = CosS3Client(CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=None, Scheme="https"))

    def upload_file(self, bucket, key, local_path):
        for i in range(0, 10):
            try:
                self.client.upload_file(
                    Bucket=bucket,
                    Key=key,
                    LocalFilePath=local_path
                )
                return key
            except CosClientError or CosServiceError as e:
                print(e)

    def get_url(self, bucket, key):
        return self.client.get_object_url(
            Bucket=bucket,
            Key=key
        )

# secret_id = "AKIDpWM4c4Tk0ARhnhS5XpxVFpMpBQYVGYkq"
# secret_key = "50uiOrEXiqts1znR4ZrumW5TfkcGgHdl"
# region = 'ap-guangzhou'
# bucket = "aiclub-1258834572"
#
# # cos = Cos(secret_id=secret_id, secret_key=secret_key, region=region)
# # cos.upload_file(bucket=bucket, key='firstfly.jpg', local_path='/Users/jiayifei/Downloads/破冰表情包 预览图/好棒.png')
# # cos.get_url(bucket, "firstfly.jpg")